/**
 * Filename: PreviewAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/19 14:54
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.github.postsanf.yinian.view.FrePhotoView;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;

public class ZoomBigAdapter extends PagerAdapter {
    private Activity context;
    private List<String> picUrls;
    private ArrayList<View> picViews;
    private Callback mCallback;

    public ZoomBigAdapter(Activity context, List<String> picUrls, Callback callback) {
        this.context = context;
        this.picUrls = picUrls;
        this.mCallback = callback;
    }

    @Override
    public int getCount() {
        return picUrls.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    public interface Callback {
        //        public void click(View v);
        public void onPhotoTap(View view, float x, float y);
    }


    @Override
    public View instantiateItem(ViewGroup container, final int position) {
        String url = picUrls.get(position);
        if (!url.startsWith("http")) {
            url = "file://" + Uri.parse(url);
        }
        final FrePhotoView photoDraweeView = new FrePhotoView(context);
        photoDraweeView.setImageUri(url, null,context);
        photoDraweeView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                mCallback.onPhotoTap(view, x, y);
            }
        });
        container.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        return photoDraweeView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}